﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmUsers : DevExpress.XtraEditors.XtraForm
    {
        public FrmUsers()
        {
            InitializeComponent();
        }

        Operator op = new Operator();

        private void FrmUsers_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            try
            {
                DataTable dt = new DataTable();
                string sql_a = "select user_id 编号,user_no 用户工号, user_name 用户姓名, user_sex 用户性别,user_power 用户权限,user_telephone 用户电话, user_address 用户地址  from users";
                dt = op.getDataTable(sql_a, "users");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }

            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from users where user_id='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "users");
                this.txtBoxID.Text = dt_1.Rows[0]["user_id"].ToString();
                this.txtBoxUserNo.Text = dt_1.Rows[0]["user_no"].ToString();
                this.txtBoxUserName.Text = dt_1.Rows[0]["user_name"].ToString();
                this.comBoxUserSex.Text = dt_1.Rows[0]["user_sex"].ToString();
                this.txtBoxUserPwd.Text = dt_1.Rows[0]["user_pwd"].ToString();
                this.comBoxPower.Text = dt_1.Rows[0]["user_power"].ToString();
                this.txtBoxTel.Text = dt_1.Rows[0]["user_telephone"].ToString();
                this.txtBoxAdd.Text = dt_1.Rows[0]["user_address"].ToString();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtBoxUserNo.ReadOnly = false;
            this.txtBoxUserName.ReadOnly = false;
            this.txtBoxUserPwd.ReadOnly = false;
            this.comBoxPower.Enabled = true;
            this.txtBoxAdd.ReadOnly = false;
            this.txtBoxTel.ReadOnly = false;
            this.txtBoxID.Text = "";
            this.txtBoxUserNo.Text = "";
            this.txtBoxUserName.Text = "";
            this.comBoxUserSex.Text = "";
            this.txtBoxUserPwd.Text = "";
            this.txtBoxAdd.Text = "";
            this.txtBoxTel.Text = "";
            this.txtBoxUserNo.Focus();
        }

        private void btnGiveUp_Click(object sender, EventArgs e)
        {
            if (this.txtBoxUserName.ReadOnly == true)
            {
                return;
            }
            this.txtBoxID.ReadOnly = true;
            this.txtBoxUserNo.ReadOnly = true;
            this.txtBoxUserName.ReadOnly = true;
            this.txtBoxUserPwd.ReadOnly = true;
            this.txtBoxAdd.ReadOnly = true;
            this.txtBoxTel.ReadOnly = true;

            this.txtBoxID.Text = "";
            this.txtBoxUserNo.Text = "";
            this.txtBoxUserName.Text = "";
            this.comBoxUserSex.Text = "";
            this.txtBoxUserPwd.Text = "";
            this.txtBoxAdd.Text = "";
            this.txtBoxTel.Text = "";
            this.comBoxPower.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (this.txtBoxID.Text == "")
            {
                MessageBox.Show("请选择需要修改的内容！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            this.txtBoxUserNo.ReadOnly = false;
            this.txtBoxUserName.ReadOnly = false;
            this.txtBoxUserPwd.ReadOnly = false;
            this.txtBoxAdd.ReadOnly = false;
            this.txtBoxTel.ReadOnly = false;
            this.txtBoxUserName.Focus();
        }

        private void Save() 
        {
            try
            {
                string c1 = "";
                string c2 = "";
                string c3 = "";
                string c4 = "";
                string c5 = "";
                string c6 = "";
                string c7 = "";
                string c8 = "";
                string c11 = "";
                DataTable dt1 = new DataTable();
                dt1 = op.getDataTable("select max(user_id) from users", "users");
                if (dt1 != null && dt1.Rows.Count > 0)
                {
                    c11 = dt1.Rows[0][0].ToString();
                    c11 = Convert.ToString(Convert.ToInt16(c11) + 1);
                }
                c1 = txtBoxID.Text.Trim();
                c2 = txtBoxUserNo.Text.Trim();
                c3 = txtBoxUserName.Text.Trim();
                c4 = comBoxUserSex.Text.Trim();
                c5 = txtBoxUserPwd.Text.Trim();
                c6 = comBoxPower.Text.Trim();
                c7 = txtBoxTel.Text.Trim();
                c8 = txtBoxAdd.Text.Trim();

                if (txtBoxUserNo.ReadOnly == true)
                    return;
                if (txtBoxID.Text.Trim() == "")
                {

                    DataTable dt2 = new DataTable();
                    dt2 = op.getDataTable("select * from users where user_no ='" + c2 + "'", "users");
                    if (dt2.Rows.Count > 0)
                    {
                        MessageBox.Show("已存在相同工号，请重新输入！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxUserNo.Text == "")
                    {
                        MessageBox.Show("用户工号不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxUserName.Text == "")
                    {
                        MessageBox.Show("用户名不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (txtBoxUserPwd.Text == "")
                    {
                        MessageBox.Show("用户密码不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    string sql_a = "insert into users(user_no, user_name, user_sex,user_pwd, user_power,user_telephone,user_address) values ('" + c2 + "','" + c3 + "','" + c4 + "','" + c5 + "','" + c6 + "','" + c7 + "','" + c8 + "')";
                    op.ExecuteSql(sql_a);
                    MessageBox.Show("保存成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    txtBoxUserNo.ReadOnly = true;
                    txtBoxUserName.ReadOnly = true;
                    txtBoxUserPwd.ReadOnly = true;
                    comBoxPower.Enabled = true;
                    txtBoxAdd.ReadOnly = true;
                    txtBoxTel.ReadOnly = true;

                    txtBoxID.Text = "";
                    txtBoxUserNo.Text = "";
                    txtBoxUserName.Text = "";
                    comBoxUserSex.Text = "";
                    txtBoxUserPwd.Text = "";
                    txtBoxAdd.Text = "";
                    txtBoxTel.Text = "";
                    comBoxPower.Text = "";

                    DataTable dt = new DataTable();
                    string sql_b = "select user_id 编号,user_no 用户工号, user_name 用户姓名, user_sex 用户性别, user_power 用户权限, user_telephone 用户电话,user_address 用户地址 from users";
                    dt = op.getDataTable(sql_b, "users");
                    dataGridView1.DataSource = dt;
                    return;
                }


                string sql_1 = "update users set user_no='" + c2 + "',user_name='" + c3 + "',user_sex='" + c4 + "',user_pwd='" + c5 + "',user_power='" + c6
                    + "',user_telephone='" + c7 + "',user_address='" + c8 + "' where user_id='" + c1 + "'";
                op.ExecuteSql(sql_1);

                MessageBox.Show("修改成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                txtBoxUserNo.ReadOnly = true;
                txtBoxUserName.ReadOnly = true;
                txtBoxUserPwd.ReadOnly = true;
                comBoxPower.Enabled = false;
                txtBoxAdd.ReadOnly = true;
                txtBoxTel.ReadOnly = true;

                txtBoxID.Text = "";
                txtBoxUserNo.Text = "";
                txtBoxUserName.Text = "";
                comBoxUserSex.Text = "";
                txtBoxUserPwd.Text = "";
                txtBoxAdd.Text = "";
                txtBoxTel.Text = "";
                comBoxPower.Text = "";

                DataTable dt11 = new DataTable();
                string sql_2 = "select user_id 编号,user_no 用户工号, user_name 用户姓名, user_sex 用户性别,user_power 用户权限, user_telephone 用户电话, user_address 用户地址 from users";
                dt11 = op.getDataTable(sql_2, "users");
                dataGridView1.DataSource = dt11;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from users where user_id='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "users");
                this.txtBoxID.Text = dt_1.Rows[0]["user_id"].ToString();
                this.txtBoxUserNo.Text = dt_1.Rows[0]["user_no"].ToString();
                this.txtBoxUserName.Text = dt_1.Rows[0]["user_name"].ToString();
                this.comBoxUserSex.Text = dt_1.Rows[0]["user_sex"].ToString();
                this.txtBoxUserPwd.Text = dt_1.Rows[0]["user_pwd"].ToString();
                this.comBoxPower.Text = dt_1.Rows[0]["user_power"].ToString();
                this.txtBoxAdd.Text = dt_1.Rows[0]["user_address"].ToString();
                this.txtBoxTel.Text = dt_1.Rows[0]["user_telephone"].ToString();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtBoxID.Text == "")
                {
                    MessageBox.Show("请选择需要删除的内容！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (MessageBox.Show("确定需要删除用户[ " + this.txtBoxUserName.Text.Trim() + " ]吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    op.ExecuteSql("delete from  users where user_id='" + this.txtBoxID.Text.Trim() + "'");
                    DataTable dt = new DataTable();
                    string sql = "select user_id 编号,user_no 用户工号, user_name 用户姓名,user_sex 用户性别,user_power 用户权限, user_telephone 用户电话, user_address 用户地址 from users";
                    dt = op.getDataTable(sql, "users");
                    dataGridView1.DataSource = dt;
                    MessageBox.Show("删除成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message);
                return; 
            }
        }

        //private void btnRefresh_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        txtBoxUserNo.ReadOnly = true;
        //        txtBoxUserName.ReadOnly = true;
        //        txtBoxUserPwd.ReadOnly = false;
        //        comBoxPower.Enabled = false;
        //        txtBoxAdd.ReadOnly = true;
        //        txtBoxTel.ReadOnly = true;

        //        txtBoxID.Text = "";
        //        txtBoxUserNo.Text = "";
        //        txtBoxUserName.Text = "";
        //        comBoxUserSex.Text = "";
        //        txtBoxUserPwd.Text = "";
        //        txtBoxAdd.Text = "";
        //        txtBoxTel.Text = "";
        //        comBoxPower.Text = "";

        //        DataTable dt = new DataTable();
        //        string sql = "select user_id 编号,user_no 用户工号, user_name 用户姓名,user_sex 用户性别,user_power 用户权限,user_telephone 用户电话,user_address 用户地址 from users";
        //        dt = op.getDataTable(sql, "users");
        //        dataGridView1.DataSource = dt;
        //    }
        //    catch (Exception ex) 
        //    { 
        //        MessageBox.Show(ex.Message);
        //        return; 
        //    }
        //}


        private void FrmUsers_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                Save();
            }
            
        }

        private void tb_UsersSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select user_id 编号,user_no 用户工号, user_name 用户姓名,user_sex 用户性别,user_power 用户权限,user_telephone 用户电话,user_address 用户地址 from users where user_no like '%" + this.txtBoxUsersSearch.Text.Trim() + "%'or user_name like '%" + this.txtBoxUsersSearch.Text.ToUpper() + "%' order by user_id";
                dt = op.getDataTable(sql, "users");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        
    }
}
